package com.car.carcontrol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.car.carcontrol.base.BaseFragment;
import com.easy.carpanel.uart.uartJnit;


//灯光
public class Fragment_First extends BaseFragment {
    public static String TAG = "1"; //TODO 用于旋转，有待改进

    private FrameLayout v;
    private Context context;


    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;    //线程开关
    Thread thread;  //串口读取线程

    Object[] allParam = new Object[]{

            new Object[]{"iv", new Rect(373*width/1280-width_d, 151*height/480-height_d, 777*width/1280, 285*height/480), R.drawable.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "", 0},
            new Object[]{"iv", new Rect(515*width/1280-width_d, 194*height/480-height_d, 65*width/1280, 60*height/480), R.drawable.light, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "", 0},
            new Object[]{"iv", new Rect(520*width/1280-width_d, 352*height/480-height_d, 50*width/1280, 52*height/480), R.drawable.light_fw, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "", 0},

            new Object[]{"iv", new Rect(596*width/1280-width_d, 198*height/480-height_d, 58*width/1280, 52*height/480), R.drawable.light_level_0, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.drawable.light_level_0},
            new Object[]{"iv", new Rect((596+70)*width/1280-width_d, 198*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_level_1, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.drawable.light_level_1},
            new Object[]{"iv", new Rect((596+70+60)*width/1280-width_d, 198*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_level_2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.drawable.light_level_2},
            new Object[]{"iv", new Rect((596+70+60*2)*width/1280-width_d, 198*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_level_3, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.drawable.light_level_3},
            new Object[]{"iv", new Rect((596+70+60*3)*width/1280-width_d, 198*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_level_4, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.drawable.light_level_4},
            new Object[]{"iv", new Rect((596+70+60*4)*width/1280-width_d, 198*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_level_5, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.drawable.light_level_5},

            new Object[]{"iv", new Rect(596*width/1280-width_d, 350*height/480-height_d, 58*width/1280, 52*height/480), R.drawable.light_fw_level_0, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_0},
            new Object[]{"iv", new Rect((596+70)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_1, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_1},
            new Object[]{"iv", new Rect((596+70+60)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_2},
            new Object[]{"iv", new Rect((596+70+60*2)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_3, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_3},
            new Object[]{"iv", new Rect((596+70+60*3)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_4, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_4},
            new Object[]{"iv", new Rect((596+70+60*4)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_5, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_5},
            new Object[]{"iv", new Rect((596+70+60*5)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_6, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_6},
            new Object[]{"iv", new Rect((596+70+60*6)*width/1280-width_d, 350*height/480-height_d, 51*width/1280, 52*height/480), R.drawable.light_fw_level_7, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.drawable.light_fw_level_7},

            new Object[]{"tv", new Rect(398*width/1280-width_d, 208*height/480-height_d, 0, 0), "照明灯", 18, Color.WHITE},
            new Object[]{"tv", new Rect(398*width/1280-width_d, 357*height/480-height_d, 0, 0), "氛围灯", 18, Color.WHITE},
    };

    private ImageView iv_light; //照明灯
    private ImageView iv_light_fw;  //氛围灯

    private boolean isLight = false; //照明灯是否关闭
    private boolean isLight_fw = false; //氛围灯是否关闭

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        Log.e("fragment_First===0", v+"==="+container);

        initView();

        isThread = true;
        loopRead();

        return v;
    }

    @Override
    public void onDestroy() {
        Log.e("f1===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);



            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                int imgBack = (int)obj[6];
                ImageView iv = new ImageView(context);

                if(imgSrc==R.drawable.light){
                    iv_light = iv;
                }else if(imgSrc==R.drawable.light_fw){
                    iv_light_fw = iv;
                }

                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);


                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===", v+"==="+event.getAction());

                            int imgId = (int)obj[2];
                            String type2 = (String)obj[5];
                            byte[] bytes = (byte[])obj[4];

                            if(event.getAction()==1){

                                if(imgId==R.drawable.light){
                                    iv.setImageResource(R.drawable.light_0);
                                    obj[2] = R.drawable.light_0;
                                }else if(imgId==R.drawable.light_0){
                                    iv.setImageResource(R.drawable.light);
                                    obj[2] = R.drawable.light;
                                }else if(imgId==R.drawable.light_fw){
                                    iv.setImageResource(R.drawable.light_fw_0);
                                    obj[2] = R.drawable.light_fw_0;
                                }else if(imgId==R.drawable.light_fw_0){
                                    iv.setImageResource(R.drawable.light_fw);
                                    obj[2] = R.drawable.light_fw;
                                }else if(imgId==R.drawable.light_level_0){
                                    iv.setImageResource(R.drawable.light_level_0f);
                                    obj[2] = R.drawable.light_level_0f;
                                }else if(imgId==R.drawable.light_level_0f){
                                    iv.setImageResource(R.drawable.light_level_0);
                                    obj[2] = R.drawable.light_level_0;
                                }else if(imgId==R.drawable.light_fw_level_0){
                                    iv.setImageResource(R.drawable.light_fw_level_0f);
                                    obj[2] = R.drawable.light_fw_level_0f;
                                }else if(imgId==R.drawable.light_fw_level_0f){
                                    iv.setImageResource(R.drawable.light_fw_level_0);
                                    obj[2] = R.drawable.light_fw_level_0;
                                }else{
                                    iv.setImageResource(imgId);
                                }

                                if(imgId==R.drawable.light_level_0f){
                                    iv_light.setImageResource(R.drawable.light);
                                    isLight = false;
                                }else if(imgId==R.drawable.light_level_0){
                                    isLight = true;
                                }else if(imgId==R.drawable.light_level_1 && isLight){
                                    iv_light.setImageResource(R.drawable.light_1);
                                }else if(imgId==R.drawable.light_level_2 && isLight){
                                    iv_light.setImageResource(R.drawable.light_2);
                                }else if(imgId==R.drawable.light_level_3 && isLight){
                                    iv_light.setImageResource(R.drawable.light_3);
                                }else if(imgId==R.drawable.light_level_4 && isLight){
                                    iv_light.setImageResource(R.drawable.light_4);
                                }else if(imgId==R.drawable.light_level_5 && isLight){
                                    iv_light.setImageResource(R.drawable.light_5);
                                }else if(imgId==R.drawable.light_fw_level_0f){
                                    iv_light_fw.setImageResource(R.drawable.light_fw);
                                    isLight_fw = false;
                                }else if(imgId==R.drawable.light_fw_level_0){
                                    isLight_fw = true;
                                }else if(imgId==R.drawable.light_fw_level_1 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_1);
                                }else if(imgId==R.drawable.light_fw_level_2 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_2);
                                }else if(imgId==R.drawable.light_fw_level_3 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_3);
                                }else if(imgId==R.drawable.light_fw_level_4 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_4);
                                }else if(imgId==R.drawable.light_fw_level_5 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_5);
                                }else if(imgId==R.drawable.light_fw_level_6 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_6);
                                }else if(imgId==R.drawable.light_fw_level_7 && isLight_fw){
                                    iv_light_fw.setImageResource(R.drawable.light_fw_7);
                                }


                            }else{
                                if(imgId==R.drawable.light_level_0){
                                    iv.setImageResource(R.drawable.light_level_0f);
                                }else if(imgId==R.drawable.light_level_1){
                                    iv.setImageResource(R.drawable.light_level_1f);
                                }else if(imgId==R.drawable.light_level_2){
                                    iv.setImageResource(R.drawable.light_level_2f);
                                }else if(imgId==R.drawable.light_level_3){
                                    iv.setImageResource(R.drawable.light_level_3f);
                                }else if(imgId==R.drawable.light_level_4){
                                    iv.setImageResource(R.drawable.light_level_4f);
                                }else if(imgId==R.drawable.light_level_5){
                                    iv.setImageResource(R.drawable.light_level_5f);
                                }else if(imgId==R.drawable.light_fw_level_0){
                                    iv.setImageResource(R.drawable.light_fw_level_0f);
                                }else if(imgId==R.drawable.light_fw_level_1){
                                    iv.setImageResource(R.drawable.light_fw_level_1f);
                                }else if(imgId==R.drawable.light_fw_level_2){
                                    iv.setImageResource(R.drawable.light_fw_level_2f);
                                }else if(imgId==R.drawable.light_fw_level_3){
                                    iv.setImageResource(R.drawable.light_fw_level_3f);
                                }else if(imgId==R.drawable.light_fw_level_4){
                                    iv.setImageResource(R.drawable.light_fw_level_4f);
                                }else if(imgId==R.drawable.light_fw_level_5){
                                    iv.setImageResource(R.drawable.light_fw_level_5f);
                                }else if(imgId==R.drawable.light_fw_level_6){
                                    iv.setImageResource(R.drawable.light_fw_level_6f);
                                }else if(imgId==R.drawable.light_fw_level_7){
                                    iv.setImageResource(R.drawable.light_fw_level_7f);
                                }
                            }

//                            write(bytes); //TODO
                            boolean isSendy = uartJnit.getInstance().sendCmds("5AA50883D2E0000000903A"); //TODO

                            return false;
                        }
                    });

                    iv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            int imgId = (int)obj[2];
                            String type2 = (String)obj[5];
                            byte[] bytes = (byte[])obj[4];

                            Log.e("fragment_First===iv", imgId+"==="+bytes.length+"==="+bytes);//2131165321 2131165322
                        }
                    });
                }

            }

        }


    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==0){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO 串口获取车速

//                    Log.e("f1===", "===");


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}

